package service.impl;

import VO.SellListVO;
import dao.*;
import dao.impl.ClientDaoImpl;
import dao.impl.CommodityDaoImpl;
import dao.impl.SellListDaoImpl;
import dao.impl.VipDaoImpl;
import entity.Client;
import entity.Commodity;
import entity.SellList;
import entity.Vip;
import service.SellListService;

import java.math.MathContext;
import java.util.ArrayList;
import java.util.List;

public class SellListServiceImpl implements SellListService {
    private SellListDao sellListDao = new SellListDaoImpl();
    private CommodityDao commodityDao = new CommodityDaoImpl();
    private ClientDao clientDao = new ClientDaoImpl();
    private VipDao vipDao = new VipDaoImpl();
    @Override
    public List<SellListVO> getAllSells() {
        List<SellList> sellLists = sellListDao.selectSellLists();
        List<SellListVO> sellListVOS = new ArrayList<SellListVO>();
        for(SellList sellList : sellLists) {
            SellListVO sellListVO = new SellListVO();
            sellListVO.setCcid(sellList.getCcid());
            Commodity commodity = commodityDao.getCommodityById(sellList.getCid());
            sellListVO.setCname(commodity.getCname());
            sellListVO.setCount(sellList.getCount());
            sellListVO.setOriginalPrice(sellList.getSalesPrice());
            Client client = clientDao.getClientById(sellList.getClid());
            Vip vip = vipDao.getVipById(client.getVid());
            sellListVO.setSalesPrice(sellList.getSalesPrice().multiply(vip.getDiscounts(),new MathContext(2)));
            sellListVO.setClname(client.getClname());
            sellListVO.setSid(sellList.getSid());
            sellListVO.setCreateTime(sellList.getCreatetime());
            sellListVOS.add(sellListVO);
        }
        return sellListVOS;
    }

    @Override
    public void addSellList(SellList sellList) {
        CommodityDao commodityDao = new CommodityDaoImpl();
        Commodity commodity = commodityDao.getCommodityById(sellList.getCid());
        sellListDao.insertSellList(commodity,sellList);
    }
}
